package sg.edu.nus.iss.billing.gui;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.JTable;

import sg.edu.nus.iss.billing.controller.UserController;
import sg.edu.nus.iss.billing.exception.ControllerException;
import sg.edu.nus.iss.billing.gui.component.CustomizableJTable;
import sg.edu.nus.iss.billing.gui.component.ErrorDialog;
import sg.edu.nus.iss.billing.gui.component.ScrollableCustomizableJTable;
import sg.edu.nus.iss.billing.privilege.AdminRole;
import sg.edu.nus.iss.billing.privilege.AgentRole;
import sg.edu.nus.iss.billing.privilege.User;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class UserTableListingPanel extends JPanel  {
	
	private static final long serialVersionUID = 1L;
	private CustomizableJTable userTable;
	private UpdateUserPanel _parent;

	private UserController userController;
	
	public UserTableListingPanel(UpdateUserPanel parent){
		this._parent = parent;
	    userController = new UserController();
        this.setLayout(null);
        List<User> userList = new ArrayList<User>();
        
        try {
            userList = userController.getAllUsers();
        } catch (ControllerException e) {
            ErrorDialog.showControllerErrorMessageDialog(e);
        }
        
        if (userList != null && userList.size() > 0) {
            //show real table
        	Vector<String> columnNames = new Vector<String>();
            columnNames.add("Selected");
            columnNames.add("Username");
            columnNames.add("Password");
            columnNames.add("Role");
            columnNames.add("Click to Edit");
            
            String [] styles = 
            {CustomizableJTable.CHECKBOX_STYLE, 
             CustomizableJTable.NORMAL_STYLE, 
             CustomizableJTable.NORMAL_STYLE, 
             CustomizableJTable.NORMAL_STYLE,
             CustomizableJTable.BUTTON_STYLE,
            };
        	
            try {
            	userTable = new CustomizableJTable(columnNames, userList.size(), styles){
            		public void doButtonDownAction(int xcellpos, int ycellpos) {
            			String username = (String)this.getValueAt(xcellpos, 1);
            			try {
            				_parent.refreshBottomPanel(new AddEditUserPanel(username, _parent));
            			} catch (ControllerException e) {
            				ErrorDialog.showControllerErrorMessageDialog(e);
            			}
            		}
            	};
            	initTableData(userTable, userList);
            } catch (Exception e) {
            	ErrorDialog.showUnhandledErrorMessageDialog(e);
            }
            this.add(new ScrollableCustomizableJTable(userTable));
        } else {
            //show user not found. Not possible as we will at least login the system
        }
	}
	
	public void refreshTableWithAllUsers() {
	    List<User> userList = new ArrayList<User>();
        
        try {
            userList = userController.getAllUsers();
        } catch (ControllerException e) {
            ErrorDialog.showControllerErrorMessageDialog(e);
        }
        refreshTable(userList);
	}
	
	public void refreshTable(List<User> userList) {
	    userTable.removeData();
	    userTable.refreshTable(userList.size());
	    initTableData(userTable, userList);
	}
	
	public List<String> getSelectedUsernames() {
		List<String> result = new ArrayList<String>();
		for (int i=0; i < userTable.getRowCount(); i++) {
			Boolean aBool = (Boolean)userTable.getValueAt(i, 0);
			if (aBool) {
				result.add((String)userTable.getValueAt(i, 1));
			}
	    }
		return result;
	}
	
	private void initTableData(JTable jTable, List<User> userList) {
	    for (int i=0; i < userList.size(); i++) {
	        User user = userList.get(i);
	        jTable.setValueAt(new Boolean(false), i, 0);
	        jTable.setValueAt(user.getUsername(), i, 1);
	        jTable.setValueAt(user.getPassword(), i, 2);
	        if (user.getRole() instanceof AdminRole) {
	            jTable.setValueAt("Admin", i, 3);
	        } else if (user.getRole() instanceof AgentRole) {
	            jTable.setValueAt("Agent", i, 3);
	        }
	        jTable.setValueAt("Edit User", i, 4);
	    }
	}
	
}
